package top.hiai.Controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import top.hiai.interceptor.AdminInterceptor;
import top.hiai.model.Link;
import top.hiai.service.LinkService;
import top.hiai.util.FormatjsonUtil;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top,开发者：黄康权
* @Introduction 后台友情链接控制器
*/

public class AdminLinkController extends Controller {

	LinkService linkService = new LinkService();
	
	/**
	 * 后台的友情链接管理列表
	 */
	@Before(AdminInterceptor.class)
	public void linkListTable(){
        Integer page = getParaToInt("page", 1);
        Integer limit = getParaToInt("limit", 10);
        Page<Link> paginate = linkService.paginate(page, limit);
        renderJson(FormatjsonUtil.layuiPage(paginate));
	}
	/**
	 * 删除一个分类
	 */
	@Before(AdminInterceptor.class)
	public void deleteLink(){
		linkService.deleteById(getParaToInt());
		redirect("/admin/adminLinkManage");
	}
	/**
	 * 添加友情链接
	 */
	public void saveLink(){
		//int tid = getParaToInt("id");
		String name = get("name");
		String url = get("url");
		int sort = getInt("sort");
		getModel(Link.class).setName(name).setUrl(url).setSort(sort).save();
		renderJson("success",true);
	}
	/**
	 * 修改分类信息
	 */
	public void editLink(){
		int lid = getParaToInt("id");
		String name = get("name");
		String url = get("url");
		int sort = getInt("sort");
		getModel(Link.class).setLid(lid).setName(name).setUrl(url).setSort(sort).update();
		renderJson("success",true);
	}
	/**
	 * 查找一个友情链接
	 */
	public void findByLink(){
		int lid = getParaToInt("id");
		Link link = linkService.findById(lid);
		renderJson("result",link);
	}
}
